package cn.group7.padserver.controller;

import cn.group7.padserver.common.Flag;
import cn.group7.padserver.controller.responseformat.Code;
import cn.group7.padserver.controller.responseformat.Result;
import cn.group7.padserver.entity.Page;
import cn.group7.padserver.pojo.Discuss;
import cn.group7.padserver.pojo.Knowledge;
import cn.group7.padserver.service.KnowledgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author 谷汉斌
 * @description 知识控制器
 * @createTime 2024/8/2 下午9:23
 */
@Api(tags = "知识模块接口")
@RestController
@RequestMapping("/knowledges")
@CrossOrigin
public class KnowledgeController {

    @Autowired
    private KnowledgeService knowledgeService;

    /**
     * 分页展示所有知识
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return 知识列表
     */
    @ApiOperation(value = "分页展示所有知识")
    @GetMapping("/all")
    public Result selectOrders(@RequestParam("pageNum") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize) {
        Page<Knowledge> page = knowledgeService.findPage(pageNum, pageSize);
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(page)
                .flag(Flag.SUCCESS.isCode()).build();
    }

    /**
     * 根据id查询知识
     *
     * @param knowledgeId 知识id
     * @return 知识
     */
    @ApiOperation(value = "根据id查询知识")
    @GetMapping("/selectKnowledgeById/{knowledgeId}")
    public Result selectKnowledgeById(@PathVariable("knowledgeId") String knowledgeId) {
        Knowledge knowledge = knowledgeService.selectById(Integer.valueOf(knowledgeId));
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(knowledge)
                .flag(Flag.SUCCESS.isCode())
                .build();
    }


    /**
     * 分页查询关键字知识
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @param keys     关键字
     * @return 知识页
     */
    @ApiOperation(value = "分页查询关键字知识")
    @GetMapping("/selectByKeys")
    public Result selectByKeys(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                               @RequestParam("keys") String keys) {
        Page<Knowledge> page = knowledgeService.findPageByKeys(keys, pageNum, pageSize);
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(page)
                .flag(Flag.SUCCESS.isCode())
                .build();
    }

    /**
     * 查询知识的讨论信息
     *
     * @param knowledgeId 知识id
     * @return 查询结果
     */
    @ApiOperation(value = "查询知识的讨论信息")
    @GetMapping("/selectDiscuss/{knowledgeId}")
    public Result selectDiscuss(@PathVariable("knowledgeId") Integer knowledgeId) {
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(knowledgeService.selectDiscuss(knowledgeId))
                .flag(Flag.SUCCESS.isCode()).build();
    }

    /**
     * 添加讨论信息
     *
     * @param knowledgeId 知识id
     * @param content     内容
     * @return 添加结果
     */
    @ApiOperation(value = "添加讨论信息")
    @PostMapping("/addDiscuss/{knowledgeId}")
    public Result addDiscuss(@PathVariable("knowledgeId") Integer knowledgeId, @RequestParam("content") String content) {
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Discuss discuss = new Discuss();
        discuss.setKnowledgeId(knowledgeId);
        discuss.setOwnName(name);
        discuss.setCreateTime(new Date());
        discuss.setContent(content);
        if (knowledgeService.addDiscuss(discuss)) {
            return Result.builder()
                    .code(Code.ADD_SUCCESS.getCode())
                    .message(Code.ADD_SUCCESS.getInfo())
                    .flag(Flag.SUCCESS.isCode())
                    .build();
        }
        return Result.builder()
                .code(Code.ADD_FAIL.getCode())
                .message(Code.ADD_FAIL.getInfo())
                .flag(Flag.FAIL.isCode())
                .build();
    }

    /**
     * 根据登录的用户获取知识
     */
    @ApiOperation(value = "根据登录的用户获取知识")
    @GetMapping("/selectByUser")
    public Result selectByUser() {
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(knowledgeService.selectByUser(name)).build();
    }


    /**
     * 添加知识
     */
    @ApiOperation(value = "添加知识")
    @PostMapping("/addKnowledge")
    public Result addKnowledge(@RequestBody Knowledge knowledge) {
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        knowledge.setOwnName(name);
        if (knowledgeService.addKnowledge(knowledge)) {
            return Result.builder()
                    .code(Code.ADD_SUCCESS.getCode())
                    .message(Code.ADD_SUCCESS.getInfo())
                    .flag(Flag.SUCCESS.isCode())
                    .build();
        }
        return Result.builder().flag(Flag.FAIL.isCode()).code(Code.ADD_FAIL.getCode())
                .message(Code.ADD_FAIL.getInfo()).build();
    }

    /**
     * 修改知识
     */
    @ApiOperation(value = "修改知识")
    @PutMapping("/updateKnowledge")
    public Result updateKnowledge(@RequestBody Knowledge knowledge) {
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        knowledge.setOwnName(name);
        if (knowledgeService.updateKnowledge(knowledge)) {
            return Result.builder()
                    .code(Code.UPDATE_SUCCESS.getCode())
                    .message(Code.UPDATE_SUCCESS.getInfo())
                    .flag(Flag.SUCCESS.isCode())
                    .build();
        }
        return Result.builder().flag(Flag.FAIL.isCode())
                .message(Code.UPDATE_FAIL.getInfo())
                .code(Code.UPDATE_FAIL.getCode())
                .build();
    }


    // 删除知识
    @ApiOperation(value = "删除知识")
    @DeleteMapping("/deleteKnowledge/{knowledgeId}")
    public Result deleteKnowledge(@PathVariable("knowledgeId") Integer knowledgeId) {
        if (knowledgeService.deleteKnowledge(knowledgeId)) {
            return Result.builder()
                    .code(Code.DELETE_SUCCESS.getCode())
                    .message(Code.DELETE_SUCCESS.getInfo())
                    .flag(Flag.SUCCESS.isCode()).build();
        }
        return Result.builder()
                .code(Code.DELETE_FAIL.getCode())
                .message(Code.DELETE_FAIL.getInfo())
                .flag(Flag.FAIL.isCode())
                .build();
    }
}
